package eclMain;


import org.joda.time.DateTime;

/**Basic class that parses out TimeStamps within Eve log files. Can also create a Calendar if need be, but only does so when one is requested.*/
public class TimeStamp {
	private int year = -1;
	private int month = -1; //month (and *only* month) is 0-based; seriously!?
	private int date = -1;
	private int hour = -1;
	private int minute = -1;
	private int second = -1;
	private DateTime jodaTime = null;
	
	/**Creates a new TimeStamp object. Passing in a value of null will create an object with values at -1(ints) or null(objects)*/
	public TimeStamp(String timestamp) {
		if (timestamp == null) return;
		
		timestamp = timestamp.replace('[', ' '); //takes care of a raw input from the log file
		timestamp = timestamp.replace(']', ' ');
		
		//the first 3 "blank" spaces are *sometimes* returning as -1 and screwing up my parsing
		if (timestamp.charAt(0) == (char)65279) {
			char[] chars = timestamp.toCharArray();
			String cleanedTimestamp = "";
			for (int i = 3; i < chars.length; i++) {
				//remove sequence: 65279,32,32 (echo, space, space)
				cleanedTimestamp += chars[i];
			}
			timestamp = cleanedTimestamp;
		}
		String[] split = timestamp.trim().split(" "); //splits into date and time
		if (split.length < 2) return; //this line doesn't have a timestamp
		String[] date = split[0].split("\\."); //splits to year, month, day
		String[] time = split[1].split(":"); //splits to hours, minute, second
		
		this.year = Integer.parseInt(date[0]);
		this.month = Integer.parseInt(date[1]);
		this.date = Integer.parseInt(date[2]);
		this.hour = Integer.parseInt(time[0]);
		this.minute = Integer.parseInt(time[1]);
		this.second = Integer.parseInt(time[2]);
	}
	
	public int getYear() {return this.year;}
	public void setYear(int year) {this.year = year;}
	
	public int getMonth() {return this.month;}
	public void setMonth(int month) {this.month = month;}
	
	public int getDate() {return this.date;}
	public void setDate(int date) {this.date = date;}
	
	public int getHour() {return this.hour;}
	public void setHour(int hour) {this.hour = hour;}
	
	public int getMinute() {return this.minute;}
	public void setMinute(int minute) {this.minute = minute;}
	
	public int getSecond() {return this.second;}
	public void setSecond(int second) {this.second = second;}
	
	public DateTime getJodaTime() {
		if (this.jodaTime == null) {
			this.jodaTime = new DateTime(this.year, this.month, this.date, this.hour, this.minute, this.second);
		}
		return this.jodaTime;
	}
	
	/**Custom toString for this timeStamp object*/
	public String toString() {
		String month = getMonth() < 10 ? "0" + getMonth() : "" + getMonth();
		String day = getDate() < 10 ? "0" + getDate() : "" + getDate();
		String hour = getHour() < 10 ? "0" + getHour() : "" + getHour();
		String minute = getMinute() < 10 ? "0" + getMinute() : "" + getMinute();
		String second = getSecond() < 10 ? "0" + getSecond() : "" + getSecond();
		return "[" + getYear() + "." + month + "." + day + " " + hour + ":" + minute + ":" + second + "]";
	}
}
